<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\ProjectMeetingLogic;
use app\cms\model\ProjectMeetingInvitationSpeaker;
use app\cms\validate\ProjectMeetingValidate;
use support\Request;
use support\Response;

/**
 * 项目会议控制器
 */
class ProjectMeetingController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new ProjectMeetingLogic();
        $this->validate = new ProjectMeetingValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['meeting_name', ''],
            ['status', ''],
            ['is_display', ''],
            ['project_id', ''], // 哪个项目过来的传哪个项目的id
        ]);
        $query = $this->logic->search($where);
        $query->with([
            'project' => function ($query) {
                $query->field('id,project_name,project_code');
            },
            'adminUser' => function ($query) {
                $query->field('id,username,nickname')->where('status', 1);
            }
        ]);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }


    /**
     * 会议完结
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-16
     * @return void
     */
    public function projectMeetingEndActions(Request $request)
    {
        $id = $request->input('id', ''); // 会议id
        $status = $request->input('status', '');
        $stop_remarks = $request->input('stop_remarks', '');
        if ($status == 2) {
            // @todo 判断项目下会议管理不能有“进行中”状态、
            $projectMeetingInvitation = ProjectMeetingInvitationSpeaker::where('project_meeting_id', $id)->where('invitation_status', 0)->count();

            if ($projectMeetingInvitation > 0) {
                return $this->fail('有待确认中的讲者');
            }
        }
        $project = $this->logic->where('id', $id)->update([
            'status' => $status,
            'stop_remarks' => $stop_remarks ? $stop_remarks : null,
            'stop_time' => $status == -1 ? date('Y-m-d H:i:s', time()) : null,
        ]);
        if ($project) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 会议中止
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-16
     * @return void
     */
    public function projectMeetingActions(Request $request)
    {
        $id = $request->input('id', '');
        $status = $request->input('status', '');
        $stop_remarks = $request->input('stop_remarks', '');
        if ($status == 2) {
            // @todo 判断项目下会议管理不能有“进行中”状态、
            // @todo 邀请阶段不能有“待确认”状态
            // return $this->fail('项目下有邀请阶段还有待确认专家');
        }
        $project = $this->logic->where('id', $id)->update([
            'status' => $status,
            'stop_remarks' => $stop_remarks ? $stop_remarks : null,
            'stop_time' => $status == -1 ? date('Y-m-d H:i:s', time()) : null,
        ]);
        if ($project) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
}
